#include "chartpreview.h"
#include "ui_chartpreview.h"
#include "chartutils.h"

ChartPreview::ChartPreview(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChartPreview)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &ChartPreview::retranslateUiSlot);
    connect(bdf, &BllDataFlow::buildFinishedSignal, this, &ChartPreview::setWidgetParent);

    initWidget();
}

ChartPreview::~ChartPreview()
{
    delete ui;
}

void ChartPreview::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void ChartPreview::showEvent(QShowEvent *)
{
    this->activateWindow();
}

void ChartPreview::setWidgetParent()
{
    this->setParent(Project::Instance());
}

void ChartPreview::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void ChartPreview::on_btnClose_clicked()
{
    this->hide();
}

void ChartPreview::viewChart(QString chartDataFileName)
{
    ChartUtils::paintChart(chartDataFileName, xAxisExp, yAxisExp, seriesExp);
}

void ChartPreview::initWidget()
{
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
    setWindowFlags(windowFlags() | Qt::SubWindow);
    if (windowWidth >= 1280) {
        this->setGeometry(0,0,1280,800);
        this->setProperty("cssSize", "large");
    } else {
        this->setGeometry(0,0,1024,600);
    }

    QChart * chartExp = new QChart();
    xAxisExp = new QValueAxis(this);
    yAxisExp = new QValueAxis(this);
    seriesExp = new QLineSeries(this);

    QBrush brush(QColor(152, 152, 152, 255));
    xAxisExp->setTitleBrush(brush);
    xAxisExp->setTitleText(tr("脉冲(个)"));
    ui->label->setText(tr("电压(V)"));
    xAxisExp->setLabelFormat("%d");
    yAxisExp->setLabelFormat("%d");

    chartExp->setMargins(QMargins(20, -10, 20, 15));
    chartExp->addSeries(seriesExp);
    chartExp->legend()->setVisible(false);
    chartExp->addAxis(xAxisExp, Qt::AlignBottom);
    chartExp->addAxis(yAxisExp, Qt::AlignLeft);
    seriesExp->attachAxis(xAxisExp);
    seriesExp->attachAxis(yAxisExp);

    ui->chartView->setChart(chartExp);
}
